package com.maple.visit;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 字段访问器
 *
 * @author maple
 * @version 1.0
 * @since 2020-11-27 00:25
 */
@Getter
public class ColumnVisitor extends MySqlASTVisitorAdapter {

    private final List<String> tableNameList = new ArrayList<>();

    @Override
    public boolean visit(MySqlSelectQueryBlock x) {
        System.out.println("SQLSelectQueryBlock");
        if (CollUtil.isEmpty(x.getSelectList())) {
            return false;
        }
        // 获取表别名
        String alias = x.getFrom().computeAlias();
        // 若存在字段为*且没有别名的需要加上别名
//        x.getSelectList().stream().filter()
        Optional<SQLSelectItem> first = x.getSelectList().stream().findFirst();
        SQLSelectItem sqlSelectItem = first.map(SQLSelectItem::clone).orElse(null);
        if (null == sqlSelectItem) {
            return false;
        }
        //        x.getSelectList().add(new SQLSelectItem());
        return super.visit(x);
    }

    @Override
    public boolean visit(SQLAllColumnExpr x) {
        return super.visit(x);
    }
}
